using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Beta_fundies_controls_misc_archivepage : Controls.DataManagerArchiveControl
{
    protected override void ItemChanged()
    {
        if (CurrentArchive != null)
        {
            mviewArchive.SetActiveView(viewArchive);
            gridQuoteArchives.DataSource = GetArchives();
            gridQuoteArchives.DataBind();
        }
        else
        {
            mviewArchive.SetActiveView(viewError);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CurrentArchive = GetItemFromQueryString();
        }
        if (CurrentArchive != null)
        {
            Page.Title = CurrentArchive.Title + " Archives";
        }
        else
        {
            Page.Title = "Archive Not Found";
        }
    }

    private List<QuoteArchive> GetArchives()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("Select yearMonth, count(*) as TotalQuotes FROM");
        sb.AppendLine("(");
        sb.AppendLine("     SELECT CAST(YEAR(DatePosted) AS varchar(4)) + '/' + CAST(MONTH(DatePosted) AS varchar(2)) AS yearMonth");
        sb.AppendLine("     FROM fstdt_quotes");
        sb.AppendLine("     WHERE ApprovalStatus = 1");
        sb.AppendLine("     and ArchiveID = @archiveID");
        sb.AppendLine(") as cats");
        sb.AppendLine("GROUP BY yearMonth");

        string sql = sb.ToString();
        SqlCommand comm = new SqlCommand(sql, DAL.DataManagerConnection.Connection);
        comm.Parameters.AddWithValue("@archiveID", CurrentArchive.ID);

        SqlDataAdapter adapter = new SqlDataAdapter(comm);
        DataTable table = new DataTable();
        adapter.Fill(table);

        List<QuoteArchive> myQuotes = new List<QuoteArchive>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {
            QuoteArchive myQuote = new QuoteArchive();
            myQuote.YearMonth = row["YearMonth"].ToString();
            myQuote.TotalQuotes = Convert.ToInt32(row["TotalQuotes"]);
            myQuotes.Add(myQuote);
        }

        myQuotes.Sort();

        return myQuotes;
    }

    protected string GetArchiveUrl(string yearMonth)
    {
        DateTime myDate = DateTime.Parse(yearMonth);
        return Helpers.StringHelper.SafeFormat("~/fundies/default.aspx?date={0}&archive={1}", myDate.ToString("yyyy/MM"), CurrentArchive.ID);
    }

    protected class QuoteArchive : IComparable<QuoteArchive>
    {
        private string _yearMonth;
        public string YearMonth
        {
            get
            {
                return _yearMonth;
            }
            set
            {
                _yearMonth = value;
            }
        }

        private int _totalQuotes;
        public int TotalQuotes
        {
            get
            {
                return _totalQuotes;
            }
            set
            {
                _totalQuotes = value;
            }
        }

        public int CompareTo(QuoteArchive other)
        {
            DateTime thisDateTime = DateTime.Parse(YearMonth);
            DateTime otherDateTime = DateTime.Parse(other.YearMonth);
            return otherDateTime.CompareTo(thisDateTime);
        }
    }
}
